package com.bitin.adorn.common.plugin;

import com.google.common.collect.Sets;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.*;

/**
 * @author Mr.Lu
 * @since 2024/12/5 17:29
 */
@Converter
public class SetToStringConverter implements AttributeConverter<Set<String>, String> {
    private static final String SPLIT_CHAR = ",";

    @Override
    public String convertToDatabaseColumn(Set<String> attribute) {
        return Optional.ofNullable(attribute).map(element -> String.join(SPLIT_CHAR, element)).orElse(null);
    }

    @Override
    public Set<String> convertToEntityAttribute(String data) {
        return Optional.ofNullable(data).map(item -> Sets.newHashSet(item.split(SPLIT_CHAR))).orElse(Sets.newHashSet());
    }
}
